package com.demo.steammanagement.controller.admin;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.demo.steammanagement.config.response.BizException;
import com.demo.steammanagement.model.entity.Users;
import com.demo.steammanagement.model.vo.Result;
import com.demo.steammanagement.model.vo.UserVO;
import com.demo.steammanagement.service.IUsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/users")
public class UsersController {

  @Autowired
  private IUsersService usersService;

  @GetMapping
  public ResponseEntity<IPage<UserVO>> getUsersByPage(
          @RequestParam(defaultValue = "1", name = "currentPage") int pageNum,
          @RequestParam(defaultValue = "10", name = "pageSize") int pageSize,
          @RequestParam(defaultValue = "", name = "queryString") String queryString
  ) {
    IPage<Users> userPage = usersService.getUsersByPage(pageNum, pageSize, queryString);
    // 将Users转换为UserDTO，假设有一个方法可以进行转换
    IPage<UserVO> userDtoPage = userPage.convert(user -> {
      UserVO userVO = new UserVO();
      userVO.setId(user.getId());
      userVO.setAccount(user.getAccount());
      userVO.setEmail(user.getEmail());
      userVO.setGender(user.getGender());
      userVO.setPhone(user.getPhone());
      userVO.setCreatedAt(user.getCreatedAt());
      userVO.setUpdatedAt(user.getUpdatedAt());
      userVO.setStatus(user.getStatus());
      userVO.setUserRole(user.getUserRole());
      userVO.setAccountExpirationDate(user.getAccountExpirationDate());
      return userVO;
    });

    return ResponseEntity.ok(userDtoPage);
  }


  @PutMapping("/{id}")
  public Result updateUserExpirationDate(@PathVariable Long id, @RequestBody Users updatedUser) {
    if (id == 1) {
      throw new BizException("超级管理员不能被编辑");
    }
    LambdaUpdateWrapper<Users> queryWrapper = new LambdaUpdateWrapper<>();
    queryWrapper.eq(Users::getId, id);
    queryWrapper.set(Users::getUserRole, updatedUser.getUserRole());
    queryWrapper.set(Users::getAccountExpirationDate, updatedUser.getAccountExpirationDate());
    usersService.update(queryWrapper);
    return Result.success(true);
  }

  @PutMapping("/{id}/reset")
  public ResponseEntity resetPwd(@PathVariable Long id) {
    LambdaUpdateWrapper<Users> wrapper = new LambdaUpdateWrapper<>();
    wrapper.eq(Users::getId, id);
    wrapper.set(Users::getPassword, new BCryptPasswordEncoder().encode("123456"));
    usersService.update(null,wrapper);
    return ResponseEntity.ok("successfully");
  }

  @DeleteMapping("/{id}")
  public ResponseEntity<String> deleteUser(@PathVariable Long id) {
    if (id == 1) {
      return ResponseEntity.ok("超级管理员不能删除");
    }
    boolean deleted = usersService.removeById(id);
    if (!deleted) {
      return ResponseEntity.notFound().build();
    }
    return ResponseEntity.ok("User deleted successfully");
  }

  @PutMapping("/{id}/status")
  public ResponseEntity<String> updateUserStatus(@PathVariable Long id, @RequestBody UserVO userVo) {
    if (id == 1 && userVo.getStatus() == 0) {
      return ResponseEntity.badRequest().body("系统管理员不能被禁用");
    }
    Users user = usersService.getById(id);
    if (user == null) {
      return ResponseEntity.notFound().build();
    }
    user.setStatus(userVo.getStatus());
    usersService.updateById(user);
    return ResponseEntity.ok("Status updated successfully");
  }

}